home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / words.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  6KB  |  197 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include "words.h"
  5. #include "clock.h"
  6.  
  7. extern clock time_of_day;
  8.  
  9.      // This function reads a line of text from the keyboard, parses
  10.      //  it, and does some limited checking.  Only the first two
  11.      //  words are considered.
  12. void words::get_command(void)
  13. {
  14. enum word wd1, wd2;
  15.  
  16.    do {
  17.       time_of_day.inc_and_print_time();
  18.       verb = (enum word)0;
  19.       noun = (enum word)0;
  20.       read_a_line(wd1, wd2);              // Get a line from the player
  21.       if (wd1) {                          // If there is a value for wd1
  22.          if (is_a_verb(wd1)) verb = wd1;  //   it is a verb
  23.          if (is_a_noun(wd1)) noun = wd1;  //   or a noun.
  24.       }
  25.  
  26.       if (wd2) {                          // If there is a value for wd2
  27.          if (is_a_verb(wd2)) {
  28.             if (verb == 0)
  29.                verb = wd2;                // it is a verb
  30.             else {
  31.                verb = noun = (enum word)0; // Two verbs, illegal
  32.                printf("Two verbs are illegal, ignored!\n");
  33.             }
  34.          }
  35.          if (is_a_noun(wd2)) {
  36.             if (noun == 0)
  37.                noun = wd2;                //  It is a noun.
  38.             else {
  39.                verb = noun = (enum word)0;
  40.                   printf("Two nouns are illegal, ignored!\n");
  41.             }
  42.          }
  43.       }
  44.  
  45.       if ((verb == 0) && (noun != 0)) {
  46.          verb = noun = (enum word)0;
  47.          printf("A verb is required, ignored!\n");
  48.       }
  49.    } while (verb == 0);
  50. }
  51.  
  52.  
  53.  
  54.  
  55.      // This function reads words in ASCII form from the keyboard
  56.      //  ignoring any words after two have been read.  The words
  57.      //  are checked to see if they are in the dictionary as de-
  58.      //  fined by the enumeration variable named "word".
  59.  
  60. void words::read_a_line(word &wd1, word &wd2)
  61. {
  62. char string1[25], string2[25], string3[25];
  63. char last_char;
  64.  
  65.    last_char = get_an_ASCII_word(string1);    // Get first word
  66.    if (last_char != '\n') {
  67.       last_char = get_an_ASCII_word(string2); // Get second word
  68.       while (last_char != '\n') {    // Ignore all trailing words
  69.          last_char = get_an_ASCII_word(string3);
  70.       }
  71.    } else {
  72.       string2[0] = 0;                         // No second word
  73.    }
  74.  
  75.    wd1 = (enum word)find_in_dictionary(string1);
  76.    wd2 = (enum word)find_in_dictionary(string2);
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83.      // This function reads a string, after ignoring the leading
  84.      //  blanks.  The string is terminated when any character is
  85.      //  read that is not alphabetic.  All characters are converted
  86.      //  to lower case for internal use to allow typing flexibility.
  87.  
  88. int words::get_an_ASCII_word(char in_string[])
  89. {
  90. int char_count = 0;
  91. int char_found = FALSE;
  92. char c;
  93.  
  94.    for (int index = 0 ; index < 80 ; index++) {
  95.       c = tolower(getchar());
  96.       if (c == '\n') {                  // End of line found
  97.          in_string[char_count] = 0;
  98.          return c;
  99.       }
  100.       if (isalpha(c) && char_count < 25) {
  101.          in_string[char_count++] = c;
  102.          char_found = TRUE;
  103.       } else {
  104.          if (isspace(c) && !char_found)
  105.             ;                           // Ignore leading blanks
  106.          else {
  107.             in_string[char_count] = 0;  // ASCIIZ terminator
  108.             return c;
  109.          }
  110.       }
  111.    }
  112. }
  113.  
  114.  
  115.  
  116.  
  117.      // This function uses the dictionary pairs to convert the
  118.      //  ASCII input strings into the internal enumeration values.
  119.      //  This list must be maintained along with the enumerated
  120.      //  type "word".
  121.  
  122. struct dict_pair {
  123.    char dict_string[10];
  124.    word found_word;
  125. };
  126.  
  127. dict_pair pair[] = {"north"    ,north,
  128.                     "n"        ,north,
  129.                     "east"     ,east,
  130.                     "e"        ,east,
  131.                     "south"    ,south,
  132.                     "s"        ,south,
  133.                     "west"     ,west,
  134.                     "w"        ,west,
  135.                     "drop"     ,drop,
  136.                     "get"      ,get,
  137.                     "look"     ,look,
  138.                     "inventory",inventory,
  139.                     "read"     ,read,
  140.                     "buy"      ,buy,
  141.                     "help"     ,help,
  142.                     "quit"     ,quit,
  143.                     "keys"     ,keys,
  144.                     "candy"    ,candy,
  145.                     "ticket"   ,ticket,
  146.                     "money"    ,money,
  147.                     "monitor"  ,monitor,
  148.                     "paper"    ,paper,
  149.             ""         ,(enum word)0 };   // List terminator
  150.  
  151. int words::find_in_dictionary(char in_string[])
  152. {
  153. int wd;
  154. dict_pair *pointer = &pair[0];
  155.  
  156.    if (in_string[0] == 0) return 0; // No string to look up
  157.    do {
  158.       if (strcmp(in_string, pointer->dict_string) == 0)
  159.          return pointer->found_word;
  160.       pointer = pointer + 1;        // Next word in list
  161.    } while (pointer->found_word);   // End of word list
  162.    printf("I don't know what \"%s\" is.\n",in_string);
  163.    return 0;                        // Word not found in list
  164. }
  165.  
  166.  
  167.  
  168.     // Is the input word a verb?
  169. int words::is_a_verb(enum word input_word)
  170. {
  171.    return ((input_word >= north) && (input_word <= quit));
  172. }
  173.  
  174.  
  175.  
  176.     // Is the input word a noun?
  177. int words::is_a_noun(enum word input_word)
  178. {
  179.    return ((input_word >= keys) && (input_word <= paper));
  180. }
  181.  
  182.  
  183.  
  184.     // Is the input word a direction?
  185. int words::is_a_direction(enum word input_word)
  186. {
  187.    return ((input_word >= north) && (input_word <= west));
  188. }
  189.  
  190.  
  191.  
  192.     // Is the input word a operation?
  193. int words::is_an_operation(enum word input_word)
  194. {
  195.    return ((input_word >= drop) && (input_word <= quit));
  196. }
  197.